var money='';
var nums=0;
$(function(){
	slf.getSequenceChild();
	if(slf.checkSign()&&slf.schoolId!=undefined&&slf.schoolId!='a'){
		//已登录
		slf.ajax({
			url : "/course/getSchoolAllCoursePackage",
			param : {
				pageIndex : 1,
				pageSize : 20,
			},
			success:function(data){
				console.log(data)
				console.log("获取课程包")
				var coursePage='';
				if(data.obj.data.length<=0){
					coursePage += '<img src="../img/zwsj.png" alt="img" style="width:85%;margin:10px 8%">';				
				}else{
					 courseData=data.obj.data;
					 for(var i=0,b=data.obj.data.length;i<b;i++){
						if(courseData[i].course_type==2){
							//只查课包的
							coursePage += '<li class="appointLi borT-EF borB-EF" style="margin-top:10px;">'
								coursePage += '<div class="appointDivs am-g">'   
								coursePage += '<div class="curriculumImg am-u-sm-4 pad-0 am-u-md-3" onclick="javascript:window.location.href=\'basepkg.html?coursePage='+courseData[i].id+'\'">' 
								if(courseData[i].cover_picture==undefined||courseData[i].cover_picture==''||courseData[i].cover_picture==null){
									var coverImg=slf.defau.courseIcon(courseData[i].id)
									coursePage +='<img src="'+coverImg+'" /></div>'
								}else{
									coursePage += '<img src="'+courseData[i].cover_picture+'" /></div>'
								}    
								coursePage += '<ul class="appointMess am-u-sm-8 pad-0 am-u-md-9">'      
							    coursePage += '<li class="titleLi" onclick="javascript:window.location.href=\'basepkg.html?coursePage='+courseData[i].id+'\'">'          
							    coursePage += '<span class="titleDiv" style="font-size:14px;">'+courseData[i].name+'</span>'
							    if(courseData[i].fit_age==undefined||courseData[i].fit_age==null||courseData[i].fit_age==''||courseData[i].end_age==undefined||courseData[i].end_age==null||courseData[i].end_age==''){
									coursePage+='<span class="ageDiv"></span>' 
								}else{
									coursePage+='<span class="ageDiv">'+courseData[i].fit_age+'-'+courseData[i].end_age +'岁</span></li>'
								}          
							    coursePage += '</li>'            
							    coursePage += '<li class="am-margin-top-xs titleLi" onclick="javascript:window.location.href=\'basepkg.html?coursePage='+courseData[i].id+'\'">'            
							    if(courseData[i].briefIntroduction!=undefined){
							    	coursePage += '<span class="descs">简介：'+courseData[i].briefIntroduction+'</span>' 
							    }           
							    coursePage += '</li>'
							    coursePage+='<li class="titleLi">'          
							    coursePage+='<span class="addrs" style="width:100%;">上课地点：'+courseData[i].schoolName+'</span></li>'
							    coursePage += '<li class="getData" style="height:40px;line-height:40px;overflow:hidden;" data-price="'+courseData[i].price+'" data-discountPrice="'+courseData[i].discount_price+'"   data-coursePageid="'+courseData[i].id+'" >'
						    	if(courseData[i].discount_price==undefined||courseData[i].discount_price==null||courseData[i].discount_price==''){
						    		coursePage += '<span style="font-size:14px;"><span style="color:#FFAB2A">'+courseData[i].price+'</span>立方币</span>'
						    	}else{
						    		coursePage += '<span style="font-size:14px;"><span style="color:#FFAB2A">'+courseData[i].discount_price+'</span>立方币</span>'
						    	}
							    if(courseData[i].isBuy=='Y'){
							    	coursePage += '<span class="classStatus" style="height:25px;line-height:25px;margin-top:8px;background:#FFFBF2;color:#29A531;border:none;text-align:right">已购买</span>' 
						    	}else if(courseData[i].isBuy=='N'&& courseData[i].up_down_state==1){
						    		coursePage += '<span class="classStatus buyBtns" style="height:25px;line-height:25px;margin-top:8px;background:#FFAB2A" onclick="buyCoursePage(this)">立即购买</span>'
						    	}else{
						    		coursePage += '<span class="classStatus qzgm_btn" style="height:25px;line-height:25px;margin-top:8px;background:#FFAB2A">立即购买</span>'
						    	} 
							                
							    coursePage += '</li></ul></div></li>' 
						}
					}
				}
				if(coursePage==''||coursePage==undefined||coursePage==null){
					coursePage +='<img src="../img/zwsj.png" alt="img" style="width:85%;margin:10px 8%">'
				}
				$('#couserDiv').html(coursePage);
				
			},error:function(){
				console.log("失败")
			}
			
		});
	}else{
		//未登录
		slf.ajax({
			url : "/system/getAloneCoursePage",
			type:'get',
			success:function(data){
				console.log(data)
				console.log("获取未登录返回课程包")
				var coursePage='';
				if(data.obj.length<=0){
					coursePage += '<img src="../img/zwsj.png" alt="img" style="width:85%;margin:10px 8%">';				
				}else{
					 courseData=data.obj;
					for(var i=0,b=data.obj.length;i<b;i++){
						coursePage += '<li class="appointLi borT-EF borB-EF" style="margin-top:10px;">'
						coursePage += '<div class="appointDivs am-g">'   
						coursePage += '<div class="curriculumImg am-u-sm-4 pad-0 am-u-md-3" onclick="javascript:window.location.href=\'basepkg.html?coursePage='+courseData[i].coursePageId+'&order=y\'">' 
						if(courseData[i].coverPicture==undefined||courseData[i].coverPicture==''||courseData[i].coverPicture==null){
							var coverImg=slf.defau.courseIcon(courseData[i].coursePageId)
							coursePage +='<img src="'+coverImg+'" /></div>'
						}else{
							coursePage += '<img src="'+courseData[i].coverPicture+'" /></div>'
						}     
						coursePage += '<ul class="appointMess am-u-sm-8 pad-0 am-u-md-9">'      
					    coursePage += '<li class="titleLi" onclick="javascript:window.location.href=\'basepkg.html?coursePage='+courseData[i].coursePageId+'&order=y\'">'          
					    coursePage += '<span class="titleDiv" style="font-size:14px;">'+courseData[i].name+'</span>'
					    if(courseData[i].fit_age==undefined||courseData[i].fit_age==null||courseData[i].fit_age==''||courseData[i].end_age==undefined||courseData[i].end_age==null||courseData[i].end_age==''){
							coursePage+='<span class="ageDiv">3-6岁</span>' 
						}else{
							coursePage+='<span class="ageDiv">'+courseData[i].fit_age+'-'+courseData[i].end_age +'岁</span></li>'
						}          
					    coursePage += '</li>'            
					    coursePage += '<li class="am-margin-top-xs titleLi" onclick="javascript:window.location.href=\'basepkg.html?coursePage='+courseData[i].coursePageId+'&order=y\'">'            
					    if(courseData[i].briefIntroduction!=undefined){
					    	coursePage += '<span class="descs">简介：'+courseData[i].briefIntroduction+'</span>' 
					    }else{
					    	coursePage += '<span class="descs"></span>'
				    	}                
					    coursePage += '</li>' 
					    coursePage+='<li class="titleLi">'          
						coursePage+='<span class="addrs" style="width:100%;">上课地点：'+courseData[i].schoolName+'</span></li>'
					    coursePage += '<li class="getData" style="height:40px;line-height:40px;overflow:hidden;" data-price="'+courseData[i].price+'" data-discountPrice="'+courseData[i].discount_price+'"   data-coursePageid="'+courseData[i].id+'" >'
				    	if(courseData[i].discountPrice==undefined||courseData[i].discountPrice==null||courseData[i].discountPrice==''){
				    		coursePage += '<span style="font-size:14px;"><span style="color:#FFAB2A">'+courseData[i].price+'</span>立方币</span>'
				    	}else{
				    		coursePage += '<span style="font-size:14px;"><span style="color:#FFAB2A">'+courseData[i].discountPrice+'</span>立方币</span>'
				    	}
				    	coursePage += '<span class="classStatus" style="height:25px;line-height:25px;margin-top:8px;background:#FFAB2A" onclick="buyCoursePage(this)">立即购买</span>'
				    	coursePage += '</li></ul></div></li>' 
						
					}
				}
				$('#couserDiv').html(coursePage);
				
			},error:function(){
				console.log("失败")
			}
			
		});
	}
	
		
});

function buyCoursePage(obj){
	$(".zcc").removeClass("am-hide")
	setTimeout(function(){
		_buyCoursePage(obj)
	},1)
}
function _buyCoursePage(obj){
	nums++;
	if(nums==1){
		var childMess=slf.getSequenceChild();
		if(slf.checkSign()){
			//已登录
			if(slf.schoolId=='a'||slf.schoolId==undefined||slf.schoolId==''){
				$(".zcc").addClass("am-hide")
				var messPrompt="请先完善孩子信息，是否现在去完善？"
				$("#class_model .am-modal-bd").html(messPrompt)
				$("#class_model").modal({
					closeViaDimmer: false,
					onConfirm: function(options) {
						window.location.href = "personal/child_list.html"
			        },
				});
				nums=0;
			}else{
				if(childMess.state==0){//无期望上课时间
					console.log("无孩子期望上课时间")
					$(".zcc").addClass("am-hide")
					$("#class_model .am-modal-bd").html("您还未选择孩子期望上课时间段，是否现在去完善？")
					$("#class_model").modal({
						relatedTarget: this,
						closeViaDimmer: false,
						onConfirm: function(options) {
							window.location.href = "personal/add_child.html?userId="+childMess.userId+"&childrenId="+childMess.childrenId+""; //去其网上可时间  
						}
					})
					nums=0;
				}else if(childMess.state==1){//you期望上课时间
					console.log("有孩子期望上课时间")
					var money=$(obj).parents(".getData").attr('data-price');
					var discountPrice=$(obj).parents(".getData").attr('data-discountPrice');
					var coursePageid=$(obj).parents(".getData").attr('data-coursePageid');
					var updatehtml='<div class="text-03C481 am-text-center">已购买</div>';
					console.log(discountPrice)
					var updateDOM=$(obj).parent('span');
					if(discountPrice==null||discountPrice==''||discountPrice==undefined||discountPrice=="undefined"){
						console.log("无折扣价")
						buyModel.buyCoursePage({money:money,courseId:coursePageid,htmlmodel:updatehtml,updateDOM:updateDOM});
					}else{
						console.log("有折扣价")
						buyModel.buyCoursePage({money:discountPrice,courseId:coursePageid,htmlmodel:updatehtml,updateDOM:updateDOM});
					}
					nums=0;
				}
			}
		}else{
			$(".zcc").addClass("am-hide")
			var messPrompt="此功能仅对登录用户开放，是否需要登录？"
			$(".operationModalBody").html(messPrompt)
			$("#operationModal").modal({
				closeViaDimmer: false,
				onConfirm: function(options) {
					window.location.href = "login.html"
		        },
		        onCancel: function() {
		          
		        }
			})
			nums=0;
		}
	}
}